<template>
  <div class="login_con">
    <h2 class="titleh1" v-if="single">{{$t('header.route[11]')}}</h2>
    <h2 class="titleh1" v-else>{{$t('regz[0]')}}</h2>
    <section class="frommain" v-if="single">
      <div class="tabs" v-if="false">
        <!-- <div class="item" :class="{active: activeTab == 1}" @click="activeTab =1" >手机注册</div> -->
        <!-- <div class="item" :class="{active: activeTab == 2}" @click="activeTab =2" >邮箱注册</div> -->
        <el-button :type=" activeTab == 1? 'primary': 'info'" @click="activeTab =1" >{{$t('register[0]')}}</el-button>
        <el-button :type=" activeTab == 2? 'primary': 'info'" @click="activeTab =2" >{{$t('register[1]')}}</el-button>
      </div>
      <div class="fromcont">
        <el-form
          label-position="top"
          label-width="80px"
          :rules="rules"
          ref="ruleForm"
          :model="formLabelAlign"
        >
          <el-form-item :label="$t('register[2]')" prop="phone" v-if="activeTab == 1">
            <div style="display: flex">
              <el-select v-model="formLabelAlign.area_code" style="width: 30%" v-show="isPhone">
                <el-option
                  v-for="(item,index) in area_codes"
                  :key="index"
                  :label="item.country"
                  :value="index"
                ></el-option>
              </el-select>
              <div style="width: 5%" v-show="isPhone"></div>
              <el-input style="flex:1" v-model="formLabelAlign.phone" :placeholder="$t('register[3]')"></el-input>
            </div>
          </el-form-item>
          <el-form-item :label="$t('register[4]')" prop="phone" v-else>
            <el-input style="flex:1" v-model="formLabelAlign.phone" :placeholder="$t('register[5]')"></el-input>
          </el-form-item>
          <el-form-item :label="$t('regz[2]')" prop="password">
            <el-input v-model="formLabelAlign.password" :placeholder="$t('reg[5]')" show-password></el-input>
          </el-form-item>
          <el-form-item :label="$t('mobile[6]')" prop="password_confirmation">
            <el-input
              v-model="formLabelAlign.password_confirmation"
              :placeholder="$t('regz[3]')"
              show-password
            ></el-input>
          </el-form-item>
          <el-form-item :label="$t('regz[4]')" prop="recommend">
            <el-input v-model="formLabelAlign.recommend" :placeholder="$t('regz[5]')"></el-input>
          </el-form-item>
        </el-form>
        <div class="register">
          <p>
            {{$t('regz[6]')}}
            <router-link :to="{name:'adoutusclause',query:{id: 5}}">{{$t('regz[7]')}}</router-link>
            {{$t('regz[8]')}}
            <router-link :to="{name:'adoutusclause',query:{id: 4}}">{{$t('regz[9]')}}</router-link>，
            <router-link :to="{name:'adoutusclause',query:{id: 9}}">{{$t('regz[10]')}}</router-link>
          </p>
        </div>
        <el-button type="primary" size="medium" @click="open()">{{$t('header.route[11]')}}</el-button>
        <div class="fromcontfooter">
          {{$t('regz[11]')}}
          <router-link to="login">{{$t('regz[12]')}}</router-link>
        </div>
      </div>
    </section>

    <section class="frommain" v-else>
      <h3 class="title" style="color: #ffffff">
        {{$t('regz[13]')}}
        <br />
        {{formLabelAlign.phone}}
      </h3>
      <div class="fromcont">
        <el-form label-position="top" label-width="80px" :model="formLabelAlign">
          <el-form-item :label="$t('mobile[9]')">
            <el-input v-model="formLabelAlign.v_code" :placeholder="$t('regz[14]')"></el-input>
          </el-form-item>
        </el-form>
        <el-button type="primary" size="medium" @click="handleSubmit()">{{$t('regz[15]')}}</el-button>
        <div class="fromcontfooter">
          {{$t('regz[16]')}}
          <router-link to="login">{{$t('regz[17]')}}</router-link>
        </div>
      </div>
    </section>

    <div class="captcha"></div>
  </div>
</template>

<script>
import { register, captcha, areaCodes } from "@/api/user";
import { EmailSms, Passwordencryption } from "@/libs/util";
// import { mapActions } from 'vuex'
// import { setToken } from "@/libs/util";
export default {
  name: "login",
  data() {
    var validatePass1 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error(this.$t("reg[9]")));
      } else if (EmailSms(value)) {
        this.isPhone = false;
        if (this.$public.email(value)) {
          callback();
        } else {
          callback(new Error(this.$t("regz[18]")));
        }
      } else {
        this.isPhone = true;
        if (this.$public.phone(value)) {
          callback();
        } else {
          callback(new Error(this.$t("regz[19]")));
        }
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error(this.$t("regz[20]")));
      } else if (this.$public.pwds(value)) {
        callback();
      } else {
        callback(new Error(this.$t("regz[21]")));
      }
    };
    var validatePass3 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error(this.$t("regz[20]")));
      } else if (value !== this.formLabelAlign.password) {
        callback(new Error(this.$t("regz[22]")));
      } else {
        callback();
      }
    };
    return {
      formLabelAlign: {
        phone: "",
        v_code: "",
        recommend: "",
        password: "",
        password_confirmation: "",
        area_code: 0
      },
      rules: {
        phone: [{ validator: validatePass1, trigger: "blur" }],
        password: [{ validator: validatePass2, trigger: "blur" }],
        password_confirmation: [{ validator: validatePass3, trigger: "blur" }],
        recommend: [
          { required: false, message: this.$t("regz[24]"), trigger: "change" }
        ]
      },
      area_codes: [],
      isPhone: true,
      single: true,
      activeTab: 1
    };
  },
  methods: {
    handleSubmit() {
      let data = {
        username:  this.formLabelAlign.phone,
        v_code: this.formLabelAlign.v_code,
        invite_code: this.formLabelAlign.recommend,
        area_code: this.area_codes[this.formLabelAlign.area_code].code,
        password: this.formLabelAlign.password,
        password_confirmation: this.formLabelAlign.password_confirmation,
      }
      
      if (!data.v_code) {
        this.$message({
          message: this.$t("regz[24]"),
          type: "warning"
        });
        return false;
      }
     
      register(Passwordencryption(data))
        .then(res => {
            this.$message({
              message:'操作成功',
              type: "success"
            });
            setTimeout(() => {
              this.$router.push({ path: "/login" });
            }, 800);
         
        })
        .catch(err => {
         
        });
    },
    open() {
      this.$refs["ruleForm"].validate(valid => {
        if (valid) {
          var _data = {
            type: "1",
            from: "register",
            phone: this.formLabelAlign.phone,
            area_code: this.area_codes[this.formLabelAlign.area_code].code
          };

          if (EmailSms(_data.phone)) {
            delete _data.area_code;
          }
          this.$public.captcha(_data, this);
        } else {
          return false;
        }
      });
    },
    typecaptcha() {
      this.single = false;
    }
  },
  created() {
    areaCodes().then(res => {
      this.area_codes = res;
      this.formLabelAlign.area_code = 0;
    });
  }
};
</script>

